//
//  ViewController.m
//  02-代码实现Autolayout-VFL
//
//  Created by J on 16/2/19.
//  Copyright © 2016年 J. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIView* blueView = [UIView new];
    blueView.backgroundColor = [UIColor orangeColor];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;//禁用AutoresizingMask
    [self.view addSubview:blueView];
    
    
    UIView* purpleView= [UIView new];
    purpleView.backgroundColor = [UIColor orangeColor];
    purpleView.translatesAutoresizingMaskIntoConstraints = NO;//禁用AutoresizingMask
    [self.view addSubview:purpleView];
    
    
    
    //添加水平方向约束
    NSNumber*margin = @20;
    NSString* vfl = @"H:|-margin-[blueView]-margin-[purpleView(==blueView)]-margin-|";
//    NSDictionary* views = @{@"blueView":blueView};
    NSDictionary* views = NSDictionaryOfVariableBindings(blueView,purpleView);//objectAndKeys 前面value 后面key
    
    
//    NSDictionary* metrics= @{@"margin":margin};
    NSDictionary* metrics =  NSDictionaryOfVariableBindings(margin);
    //NSLayoutFormatAlignAllTop|NSLayoutFormatAlignAllBottom水平方向和竖直方向上是相同的
    NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:NSLayoutFormatAlignAllTop|NSLayoutFormatAlignAllBottom metrics:metrics views:views];
    [self.view addConstraints:constraints];//与父控件有关添加到self.view上
    
    //添加竖直方向约束
    NSNumber* height = @40;
    NSString* vfl2 = @"V:[blueView(height)]-margin-|";
//    NSDictionary* metrics2= @{@"margin":margin,@"height":height};
//    NSDictionary* views2 = NSDictionaryOfVariableBindings(blueView);//objectAndKeys 前面value 后面key

    NSDictionary* metrics2 =  NSDictionaryOfVariableBindings(margin,height);
    NSArray* Vconstraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl2 options:kNilOptions metrics:metrics2 views:views];
    [self.view addConstraints:Vconstraints];//与父控件有关添加到self.view上
    
    
    
    //添加红色剩余约束
//    NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:purpleView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
//    [self.view addConstraint:topConstraint];
//    
//    NSLayoutConstraint* bottomConstraint = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:purpleView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
//    [self.view addConstraint:bottomConstraint];


}

//VFL替换
- (void)test2{
    
    UIView* blueView = [UIView new];
    blueView.backgroundColor = [UIColor orangeColor];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;//禁用AutoresizingMask
    [self.view addSubview:blueView];
    
    
    UIView* purpleView= [UIView new];
    purpleView.backgroundColor = [UIColor orangeColor];
    purpleView.translatesAutoresizingMaskIntoConstraints = NO;//禁用AutoresizingMask
    [self.view addSubview:purpleView];
    
    
    
    //添加水平方向约束
    NSNumber*margin = @20;
    NSString* vfl = @"H:|-margin-[blueView]-margin-|";
    //    NSDictionary* views = @{@"blueView":blueView};
    NSDictionary* views = NSDictionaryOfVariableBindings(blueView);//objectAndKeys 前面value 后面key
    
    
    //    NSDictionary* metrics= @{@"margin":margin};
    NSDictionary* metrics =  NSDictionaryOfVariableBindings(margin);
    
    NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:kNilOptions metrics:metrics views:views];
    [self.view addConstraints:constraints];//与父控件有关添加到self.view上
    
    //添加竖直方向约束
    NSNumber* height = @40;
    NSString* vfl2 = @"V:|-margin-[blueView(height)]";
    //    NSDictionary* views2 = @{@"blueView":blueView};
    //    NSDictionary* metrics2= @{@"margin":margin,@"height":height};
    
    NSDictionary* metrics2 =  NSDictionaryOfVariableBindings(margin,height);
    
    NSArray* Vconstraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl2 options:kNilOptions metrics:metrics2 views:views];
    [self.view addConstraints:Vconstraints];//与父控件有关添加到self.view上

}

//VFL简单使用
- (void)test{
    
    UIView* blueView = [UIView new];
    blueView.backgroundColor = [UIColor orangeColor];
    blueView.translatesAutoresizingMaskIntoConstraints = NO;//禁用AutoresizingMask
    [self.view addSubview:blueView];
    
    
    UIView* purpleView= [UIView new];
    purpleView.backgroundColor = [UIColor orangeColor];
    purpleView.translatesAutoresizingMaskIntoConstraints = NO;//禁用AutoresizingMask
    [self.view addSubview:purpleView];
    
    
    
    //添加水平方向约束
    NSString* vfl = @"H:|-10-[blueView]-20-|";
    NSDictionary* views = @{@"blueView":blueView};
    NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl options:kNilOptions metrics:nil views:views];
    [self.view addConstraints:constraints];//与父控件有关添加到self.view上
    
    //添加竖直方向约束
    NSString* vfl2 = @"V:|-20-[blueView(40)]";
    NSDictionary* views2 = @{@"blueView":blueView};
    NSArray* Vconstraints = [NSLayoutConstraint constraintsWithVisualFormat:vfl2 options:kNilOptions metrics:nil views:views2];
    [self.view addConstraints:Vconstraints];//与父控件有关添加到self.view上
    

    
}



@end
